#!/bin/bash
HEADERS="assert.h stdarg.h setjmp.h stdio.h stdlib.h string.h sys\/types.h stddef.h time.h ctype.h stdint.h"

if [ "$1" = "restore" ];then
for file in `find -type f | grep [.]c'$'`
do
	if [ ! -f "$file.orig" ];then
		echo "Skipping $file, which is already original."
		continue
	fi
	echo "Restoring $file..."
	mv "$file.orig" "$file"
done

for file in `find -type f | grep [.]h'$'`
do
	if [ ! -f "$file.orig" ];then
		echo "Skipping $file, which is already original."
		continue
	fi
	echo "Restoring $file..."
	mv "$file.orig" "$file"
done

else

for file in `find -type f | grep [.]c'$'`
do
	if [ -f "$file.orig" ];then
		echo "Skipping $file, which is already patched."
		continue
	fi
	echo "Replacing $file..."
	mv "$file" "$file.orig"
	cp "$file.orig" "$file.tmp"
	for HEADER in $HEADERS
	do
		cat "$file.tmp" | sed "/""$HEADER""/d" > "$file.tmp2"
		mv "$file.tmp2" "$file.tmp"
	done
	mv "$file.tmp" "$file"
done
for file in `find -type f | grep [.]h'$'`
do
	if [ -f "$file.orig" ];then
		echo "Skipping $file, which is already patched."
		continue
	fi
	echo "Replacing $file..."
	mv "$file" "$file.orig"
	cp "$file.orig" "$file.tmp"
	for HEADER in $HEADERS
	do
		cat "$file.tmp" | sed "/""$HEADER""/d" > "$file.tmp2"
		mv "$file.tmp2" "$file.tmp"
	done
	mv "$file.tmp" "$file"
done

fi
